package ucn.dmf83.sc.workshop1.group3.UILayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import ucn.dmf83.sc.workshop1.group3.ControlLayer.AddressBookCtrl;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Loan;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Person;

public class LoansMenu {
	
	private static AddressBookCtrl controller;
    
	private static void clear() {
		int i = 0;
		while(i < 25)
		{
			System.out.println("");
			i++;
		}
	}
	
	private static void showHeader(String menuName) {
		clear();
   	 	System.out.println("------------------------------------------------");
        System.out.println("-                                              -");
        System.out.println("-    AddressBook v0.1 by Group3 Inc.           -");
        System.out.println("-                                              -");
        System.out.println("------------------------------------------------");

        System.out.println("\n\n\f *** " + menuName +" ***\n\n");
	}
	
	// TODO Add functions, write functionality
    public static void showMenu() {
    	showHeader("Loans Menu");
         int choice = printLoanMenu();
         
         switch(choice) {
         case 1:
        	 findLoan();
         case 2:
        	 returnLoan();
         case 3:
        	 printAllLoans();
         case 4:
        	 MainMenu.showMenu();        	 
         default:
             System.out.println("Invalid selection, please try again!");
             showMenu();
         }
    }
    
    public static void showMenu(AddressBookCtrl controller1)
    {
    	controller = controller1;
    	showMenu();
    }
    
    public static int printLoanMenu() {
        System.out.println(" 1. Search");
        System.out.println(" 2. Return loan");
        System.out.println(" 3. List all");
        System.out.println(" 4. Return");

        System.out.print("\n \n Please type in the number of your requested option: ");
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String data = null;
 		try {
 			data = br.readLine();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		
 		int i = 0;
 		
 		try {
 			i = Integer.parseInt(data);
 		}
 		catch(Exception e) {
 			e.printStackTrace();
 		}
 		
        return i;
    }
    
    
    public static void printAllLoans() {
    	showHeader("Loans List");
    	ArrayList<Person> people = controller.getPeople();
    	ArrayList<Loan> loans;
    	
    	for(Iterator<Person> it = people.iterator(); it.hasNext();) {
    		loans = it.next().getLoans();
    		Person person = it.next();
    		System.out.println("\n\n------------------------------------------------");
    		System.out.println("\n\n Name: " + person.getName() + "\n\n");
    		for(Iterator<Loan> ita = loans.iterator(); it.hasNext();) {
    			Loan loan = ita.next();
    			
    			System.out.println("Title: " + loan.getCopy().getOriginal().getTitle());
    			System.out.println("Copy Serial Num: " + loan.getCopy().getSerialNum());
    			System.out.println("Borrow start: " + loan.getBorrowDate());
    			System.out.println("Period: " + loan.getPeriod() + "\n\n");
    		}
    		System.out.println("\n\n------------------------------------------------");
    	}
    	
    	Scanner sc = new Scanner(System.in);
 	   System.out.println("Press Enter to return to AddressBook menu");
 	   sc.nextLine();
 	   showMenu();
 	   sc.close();
    }
    
    public static void findLoan() {
    	showHeader("Find Loan");
    	System.out.println("Please enter the name of the person: ");
    	
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String data = null;
 		try {
 			data = br.readLine();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		
 		
 		ArrayList<Loan> loans = controller.getLoans(controller.getPerson(data));
 		showHeader("Loans Search");
 		System.out.println("Loans found for user " + data);
 		
 		if(loans == null)
 		{
 			System.out.println("No entries found");
 			showMenu();
 		}
 		else {
	 		for(Iterator<Loan> it = loans.iterator(); it.hasNext();) {
	 			Loan loan = it.next();
	 			System.out.println(controller.getPerson(data).getName() + loan.getId() + loan.getBorrowDate() + loan.getPeriod());
	 		}
	 		
	 		System.out.println("Please enter ID of Loan you want to see: ");
	 		Person person = controller.getPerson(data);
	 		data = null;
	 		try {
	 			data = br.readLine();
	 		} catch (IOException e) {
	 			e.printStackTrace();
	 		}
	 		
	 		int id = -1;
	 		try {
	 			id = Integer.parseInt(data);
	 		} catch (Exception e) {
	 			
	 		}
	 		
	 		showLoan(person.getName(), id);
 		}
    }
    
    public static void showLoan(String name, int id) {
    	showHeader("Loan Information");
    	
    	Loan loan = controller.getLoan(controller.getPerson(name), id);
    	
    	System.out.println("Title: " + loan.getCopy().getOriginal().getTitle());
    	System.out.println("Borrow date: " + loan.getBorrowDate());
    	System.out.println("Period: " + loan.getPeriod() + " days\n\n");
    	
    	System.out.println("Press 1 to return the loan, 2 to edit it, and 3 to get back to the main menu");
    	
    	Scanner sc = new Scanner(System.in);
    	int choice = sc.nextInt();
    	sc.close();
    	
    	switch(choice)
    	{
    	case 1:
    		controller.returnLoan(controller.getPerson(name), id);
    		showMenu();
    	case 2:
    		editLoan(name,id);
    	case 3:
    		showMenu();
    	default:
    		System.out.println("Invalid selection!");
    		showLoan(name, id);
    	}
    	
    }
    
    public static void editLoan(String name, int id)
    {
    	showHeader("Editing Loan " + id);
    	
    	System.out.println("Original period: " + controller.getLoan(controller.getPerson(name), id).getPeriod() + " days");
    	System.out.println("Enter new period: ");
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String data = null;
 		try {
 			data = br.readLine();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		
 		controller.getLoan(controller.getPerson(name), id).setPeriod(Integer.parseInt(data));
    	showMenu();
    }
    
    public static void returnLoan() {
    	showHeader("Loan Return");
    	
    	Scanner sc = new Scanner(System.in);
    	
    	System.out.println("Enter name of borrower: ");
    	String name = sc.nextLine();
    	
    	System.out.println("Enter loan ID: ");
    	int id = sc.nextInt();
    	
    	controller.returnLoan(controller.getPerson(name), id);
    	sc.close();
    	showMenu();
    }
    
}
